//
//  HMNetworkTools.swift
//  3.Swift中网络的封装
//
//  Created by heima on 16/4/8.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import AFNetworking


struct API {
    static let onlineHost = "https://api.weibo.com/"
    //测试环境  在上次AppStore之前 一定要取消使用这个环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    //首页数据加载连接
    static let homepage = "2/statuses/home_timeline.json"
}

//swift 中的枚举
enum HMHttpMethods: String {
    case POST = "POST"
    case GET = "GET"
}

class HMNetworkTools: AFHTTPSessionManager {
    //声明单例对象
    static let sharedTools: HMNetworkTools = {
        //设置baseURL
        let tools = HMNetworkTools(baseURL: NSURL(string: API.onlineHost))
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    //封装所有的网络请求方法 所有的网络请求都是通过这个方法和 AFN进行联系
    func request(method: HMHttpMethods,urlString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?) -> ()) {
        
        //swift中就不能够使用利用协议 欺骗Xcode
        //为了达到封装的目的 可以将相同的闭包 抽取出来,当做参数传递给AFN框架
        let success =  { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            //执行成功的回调
            finished(result: result, error: nil)
        }
        
        //定义失败的回调
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            //执行失败的回调
            finished(result: nil, error: error)
        }
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
    }
    
}


//MARK:首页相关数据加载
extension HMNetworkTools {

    func loadHomePageDate(token:String,max_id: Int64,since_id: Int64,finished: (result : AnyObject?, error: NSError? )->()){
        var parameters = ["access_token": token]
     //var max = max_id
        if max_id > 0 {
        parameters["max_id"] = "\(max_id - 1)"
        }
        if since_id > 0 {
        parameters["since_id"] = "\(since_id)"
        }
    
    //let parameters = ["access_token": token,"max_id":"\(max)"]
    request(.GET, urlString: API.homepage, parameters: parameters, finished: finished)
    
    }

}


//封装token的获取
extension HMNetworkTools {
    
    func loadAccessToken(code: String ,finished: (result : AnyObject?, error: NSError? )->()){
    
    //let urlString = "https://api.weibo.com/oauth2/access_token"
        
    let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]

    request(.POST, urlString: API.authrizon, parameters: parameters, finished: finished)
    }
//加载用户信息的数据
    func loadUserInfo(token:AnyObject , uid : AnyObject, finished: (result : AnyObject?, error: NSError? )->()){

    //let  urlstring = "https://api.weibo.com/2/users/show.json"
    let parameters = ["access_token": token, "uid": uid]
 
    request(.GET, urlString: API.access_token, parameters: parameters, finished: finished)

       

}

}

